<ui:composition template="../template/template.xhtml" xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui">
	<ui:define name="localizacao">
		<h:outputText value="&#160;" />
		<a href="listarConcursos.xhtml"> Concursos</a>
		<h:outputText value="&#160;>&#160;" />
		<a href="detalhesConcurso.xhtml">#{concursoMB.concursoSelecionado.nome}</a>
		<h:outputText value="&#160;>&#160;" />
		Disciplina: #{cadastrarDisciplinaMB.disciplinaSelecionada.nome}
		<h:outputText value="&#160;>&#160;" />
		Referencias
	</ui:define>
	<ui:define name="content">
		<h4>
			<b><h:outputText value="Inserir Referencia" /></b>
		</h4>
		<br />
		<h4>
			<b><h:outputText value="Concurso: " /></b>
			<h:outputText value="#{cadastrarDisciplinaMB.disciplinaSelecionada.concurso.nome}" />
		</h4>
		<h4>
			<b><h:outputText value="Disciplina: " /></b>
			<h:outputText value="#{cadastrarDisciplinaMB.disciplinaSelecionada.nome}" />
		</h4>
		<br />
		<br />
		<p:growl id="msgPagina" showDetail="true" sticky="false" life="4000" />
		<h:form>
			<p:panel id="panel">
			
				<table>
					<tr>
						<td><h:outputText value="Filtrar por: " /></td>
					</tr>
					<tr>
						<td><p:selectOneMenu id="filtro" value="#{referenciaMB.filtroListar}">
								<f:selectItem itemLabel="Selecione" itemValue="" />
								<f:selectItem itemLabel="Links" itemValue="link" />
								<f:selectItem itemLabel="Livros" itemValue="livro" />
								<f:selectItem itemLabel="Arquivos" itemValue="arquivo" />
								<p:ajax listener="#{referenciaMB.buscar()}" />
							</p:selectOneMenu></td>
					</tr>
				</table>
			</p:panel>
		</h:form>

		<h:form id="formAddReferencia">
			<p:commandButton id="lembrete" icon="ui-icon-plusthick" value="Adicionar nova"
				oncomplete="dlg2.show()" process="@this"
				actionListener="#{lembreteMB.selecionarConcurso(concurso)}" update=":modalDialog"
				styleClass="ui-priority-primary">
			</p:commandButton>
		</h:form>

		<h:form id="lista">
		
			<h:panelGroup rendered="#{referenciaMB.filtroListar eq 'livro'}">
				<p:dataTable value="#{referenciaMB.listaReferenciaLivro}" var="ref" id="reflivro"
					emptyMessage="Nenhuma referencia cadastrada" paginator="true" rows="10">
					paginatorTemplate="{PageLinks} {RowsPerPageDropdown}" rowsPerPageTemplate="5,10,15"
					draggableColumns="true" paginatorAlwaysVisible="false" paginatorPosition="bottom">

					<f:facet name="header">  
            			Livros Sugeridos
        			</f:facet>
					<p:ajax event="rowEdit" listener="#{referenciaMB.onEditLivro}" />
					<p:column headerText="Autor">
					<p:cellEditor>  
		                <f:facet name="output">
							<h:outputText value="#{ref.autor}" />
						</f:facet>
						<f:facet name="input">
							<p:inputText value="#{ref.autor}" style="width:100%;" />
						</f:facet>
					</p:cellEditor>
					</p:column>
					<p:column headerText="Livro">
					<p:cellEditor>  
		                <f:facet name="output">
							<h:outputText value="#{ref.livro}" />
						</f:facet>
						<f:facet name="input">
							<p:inputText value="#{ref.livro}" style="width:100%;" />
						</f:facet>
					</p:cellEditor> 
					</p:column>
					<p:column style="width:3%">
						<f:facet name="header">Editar</f:facet>
						<p:rowEditor />
					</p:column>
					<p:column>
					<f:facet name="header">Acoes</f:facet>
					<p:commandButton id="excluir" icon="ui-icon-trash" title="Excluir"
						actionListener="#{referenciaMB.selecionarReferenciaLivro(ref)}"
						oncomplete="confirmation.show()" update=":formRemoverLivro" />
					</p:column>
				</p:dataTable>
			</h:panelGroup>

			<h:panelGroup rendered="#{referenciaMB.filtroListar eq 'link'}">
				<p:dataTable value="#{referenciaMB.listaReferenciaLink}" var="ref" id="refLink"
					emptyMessage="Nenhuma referencia cadastrada" paginator="true" rows="10">
					paginatorTemplate="{PageLinks} {RowsPerPageDropdown}" rowsPerPageTemplate="5,10,15"
					draggableColumns="true" paginatorAlwaysVisible="false" paginatorPosition="bottom">

					<f:facet name="header">  
            			Links Sugeridos
        			</f:facet>

					paginatorTemplate="{PageLinks} {RowsPerPageDropdown}" rowsPerPageTemplate="5,10,15"
					editable="true" >
					
					<p:ajax event="rowEdit" listener="#{referenciaMB.onEditLink}" />
					<p:column headerText="Descricao">
					<p:cellEditor>  
		                <f:facet name="output">
						<h:outputText value="#{ref.descricaoReferencia}" />
							</f:facet>
						<f:facet name="input">
							<p:inputText value="#{ref.descricaoReferencia}" style="width:100%;" />
						</f:facet>
					</p:cellEditor>
					</p:column>
					<p:column headerText="Link">
						<h:outputLink id="link1" value="http://#{ref.link}" target="_blank">
						<p:cellEditor>  
		                	<f:facet name="output">
								<h:outputText value="#{ref.link}" />
									</f:facet>
								<f:facet name="input">
									<p:inputText value="#{ref.link}" style="width:100%;" />
								</f:facet>
						</p:cellEditor>
						</h:outputLink>
					</p:column>
					<p:column style="width:3%">
						<f:facet name="header">Editar</f:facet>
						<p:rowEditor />
					</p:column>
					<p:column>
					<f:facet name="header">Acoes</f:facet>
					<p:commandButton id="excluir" icon="ui-icon-trash" title="Excluir"
						actionListener="#{referenciaMB.selecionarReferenciaLink(ref)}"
						oncomplete="confirmation.show()" update=":formRemoverLink" />
					</p:column>
				</p:dataTable>
			</h:panelGroup>

			<h:panelGroup rendered="#{referenciaMB.filtroListar eq 'arquivo'}">
				<p:dataTable value="#{referenciaMB.listaReferenciaArquivo}" var="ref" id="refArquivo"
					emptyMessage="Nenhuma referencia cadastrada" paginator="true" rows="10"
					paginatorTemplate="{PageLinks} {RowsPerPageDropdown}" rowsPerPageTemplate="5,10,15"
					draggableColumns="true" paginatorAlwaysVisible="false" paginatorPosition="bottom">

					<f:facet name="header">  
            			Arquivos
        			</f:facet>

					<p:column headerText="Descricao">
						<h:outputText value="#{ref.nomeArquivo}" />
					</p:column>
				<p:column headerText="Baixar" style="width:10%">
						<p:commandButton id="downloadLink" value="Download" ajax="false"
							actionListener="#{baixarArquivoMB.selecionarArquivoRef(ref)}" icon="ui-icon-arrowstop-1-s">
							<p:fileDownload value="#{baixarArquivoMB.file}" />
						</p:commandButton>
					</p:column>
					<p:column>
					<f:facet name="header">Acoes</f:facet>
					<p:commandButton id="excluir" icon="ui-icon-trash" title="Excluir"
						actionListener="#{referenciaMB.selecionarReferenciaArquivo(ref)}"
						oncomplete="confirmation.show()" update=":formRemoverArquivo" />
					</p:column>
				</p:dataTable>
			</h:panelGroup>
		</h:form>

		<h:form id="formRemoverLivro">
			<p:confirmDialog id="confirmDialog"
				message="Excluir a referencia #{referenciaMB.referenciaLivro.livro}" header="Certeza?"
				severity="alert" widgetVar="confirmation">
				<p:commandButton id="confirm" value="Sim" oncomplete="confirmation.hide()"
					action="#{referenciaMB.excluirReferenciaLivro()}" update=":lista,:msgPagina" />
				<p:commandButton id="decline" value="Nao" onclick="confirmation.hide()" type="button" />
			</p:confirmDialog>
		</h:form>
		
		<h:form id="formRemoverLink">
			<p:confirmDialog id="confirmDialog"
				message="Excluir a referencia #{referenciaMB.referenciaLink.descricaoReferencia}" header="Certeza?"
				severity="alert" widgetVar="confirmation">
				<p:commandButton id="confirm" value="Sim" oncomplete="confirmation.hide()"
					action="#{referenciaMB.excluirReferenciaLink()}" update=":lista,:msgPagina" />
				<p:commandButton id="decline" value="Nao" onclick="confirmation.hide()" type="button" />
			</p:confirmDialog>
		</h:form>
		<h:form id="formRemoverArquivo">
			<p:confirmDialog id="confirmDialog"
				message="Excluir a referencia: #{referenciaMB.referenciaArquivo.nomeArquivo}" header="Certeza?"
				severity="alert" widgetVar="confirmation">
				<p:commandButton id="confirm" value="Sim" oncomplete="confirmation.hide()"
					action="#{referenciaMB.excluirReferenciaArquivo()}" update=":lista,:msgPagina" />
				<p:commandButton id="decline" value="Nao" onclick="confirmation.hide()" type="button" />
			</p:confirmDialog>
		</h:form>
		<br />

		<h:form>
			<p:commandButton actionListener="#{concursoMB.urlDetalhesConcurso()}" value="Voltar"
				icon="ui-icon-circle-triangle-w" />
		</h:form>

		<p:dialog id="modalDialog" header="Adicionar Referencia" widgetVar="dlg2" modal="true"
			height="270">
			<br />
			<h:form id="formAddRefModal">
				<p:panel>
					<h:outputText value="Selecione o tipo:" />
					<br />
					<p:growl id="msg1" showDetail="true" sticky="false" life="4000" />
					<h:selectOneMenu value="#{referenciaMB.filtroBusca}">
						<f:selectItem itemLabel="Selecione" itemValue="" />
						<f:selectItem itemLabel="Livro" itemValue="livro" />
						<f:selectItem itemLabel="Link" itemValue="link" />
						<f:selectItem itemLabel="Arquivo" itemValue="arquivo" />
						<f:ajax render="@form" />
					</h:selectOneMenu>
				</p:panel>
				<br />
				<h:outputText value="Informe os dados:" />
				<h:panelGroup rendered="#{referenciaMB.filtroBusca == 'link'}">
					<p:panel>
						<table>
							<tr>
								<td><h:outputText value="Titulo: " /></td>
								<td><p:inputText id="referencia"
										value="#{referenciaMB.referenciaLink.descricaoReferencia}" /></td>
							</tr>
							<tr>
								<td><h:outputText value="Link: " /></td>
								<td><p:inputText id="link" value="#{referenciaMB.referenciaLink.link}" /></td>
							</tr>
							<tr>
								<td><p:commandButton actionListener="#{referenciaMB.cadastrarReferenciaLink()}"
										value="Salvar" update="msg1, :formAddReferencia, formAddRefModal" oncomplete="dlg2.hide()" /></td>
							</tr>
						</table>
					</p:panel>
				</h:panelGroup>
				<h:panelGroup rendered="#{referenciaMB.filtroBusca == 'livro'}">
					<p:panel>
						<table>
							<tr>
								<td><h:outputText value="Livro: " /></td>
								<td><p:inputText id="livro" value="#{referenciaMB.referenciaLivro.livro}" /></td>
							</tr>
							<tr>
								<td><h:outputText value="Autor: " /></td>
								<td><p:inputText id="autor" value="#{referenciaMB.referenciaLivro.autor}" /></td>
							</tr>
							<tr>
								<td><p:commandButton actionListener="#{referenciaMB.cadastrarReferenciaLivro()}"
										value="Salvar" update="msg1, :formAddReferencia, formAddRefModal" oncomplete="dlg2.hide()" /></td>
							</tr>
						</table>
					</p:panel>
				</h:panelGroup>
				<h:panelGroup rendered="#{referenciaMB.filtroBusca == 'arquivo'}">
					<p:panel>
						<table>
							<tr>
								<td><p:fileUpload fileUploadListener="#{referenciaMB.adicionarArquivo}"
										label="Procurar..." mode="advanced" update="msg1,:lista" sizeLimit="10000000"
										allowTypes="/(\.|\/)(gif|jpe?g|png|pdf|doc?x|odt)$/" cancelLabel="Cancelar"
										invalidFileMessage="Arquivo no Formato Invalido"
										invalidSizeMessage="Arquivo de Tamanho Invalido" oncomplete="dlg2.hide()" /></td>
							</tr>
						</table>
					</p:panel>
				</h:panelGroup>
			</h:form>
		</p:dialog>



	</ui:define>
</ui:composition>
